/**    
* @Title: LocalCacheRepository.java  
* @Package com.kaver.spring   
* @author roc   
* @date 2019年1月30日 下午4:59:41  
* @version V1.0  
* @copyright kaver 
*/
package com.kaver.spring;

import java.io.File;
import java.util.Collection;
import java.util.Properties;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.kaver.listener.ChangeType;
import com.kaver.utils.FileUtil;
import com.kaver.utils.PropUtil;

/**
 * @ClassName: LocalCacheRepository
 * @Description: 本地缓存
 * @author roc
 * @date 2019年1月30日 下午4:59:41
 * 
 */
public class LocalCacheRepository extends AbstractConfRepository {
	private final static Logger logger = LoggerFactory.getLogger(LocalCacheRepository.class);
	private static LocalCacheRepository instance = new LocalCacheRepository();
	private static String fileName = "%s_%s.properties";
	
	
	public static LocalCacheRepository getInstance() {
		return instance;
	}
	
	
	
	@Override
	public void load() throws Exception {
		try {
			logger.debug("加载本地配置");
			File dir = new File(AppInstance.getCachePath());
			if (!dir.exists()) {
				FileUtil.createDir(AppInstance.getCachePath());
			}
			Collection<File> files = FileUtils.listFiles(dir, new String[] { "properties" }, true);
			if (!files.isEmpty()) {
			    for (File file : files) {
			        String name = file.getName();
			        if (name.startsWith(AppInstance.getAppName())) {
			            String version = name.substring(name.lastIndexOf("_") + 1, name.lastIndexOf("."));
	                    if (AppInstance.getVersion() == null) {
	                        AppInstance.setVersion(Long.valueOf(version));
	                    }
	                    Properties properties =  PropUtil.loadFileProp(file.getPath());
	                    mergeMe(properties, ChangeType.ADD);
	                    return;
			        }
			    }
			}
			logger.debug("加载本地配置成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("加载本地配置失败", e);
		}

	}

	public void restore() {
		try {
			FileUtil.delFileByPattern(AppInstance.getCachePath()+ File.separator + AppInstance.getAppName() + "_\\d+.properties");
			String filePathName = AppInstance.getCachePath() + File.separator
					+ String.format(fileName, AppInstance.getAppName(), AppInstance.getVersion());
			PropUtil.writeFileProp(this.getProperties(), filePathName);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("存储缓存失败", e);
		}
	}

	public void mergeMe(Properties properties, ChangeType changeType) {
		if (properties != null) {
			if (ChangeType.DELETE.equals(changeType)) {
				for (Object key : properties.keySet()) {
					this.getProperties().remove(key);
				}
			} else {
				for (Object key : properties.keySet()) {
					this.getProperties().put(key, properties.get(key));
				}
			}
		}
	}
	public void resetMe(Properties properties) {
		if (properties != null) {
			this.getProperties().clear();
			for (Object key : properties.keySet()) {
				this.getProperties().put(key, properties.get(key));
			}
		}
	}
}
